<?php

namespace App\Trip\Plane\Rules;

use App\Models\Plane\Rules\PlaneRefundRule;
use App\Trip\Plane\Requests\RulesRequest;
use App\Trip\Plane\Rules\Interfaces\RefundRulesInterface;
use Carbon\Carbon;
use Exception;

class SystemRefundRule implements RefundRulesInterface
{
    private RulesRequest $request;
    private              $rules;

    /**
     * @throws Exception
     */
    public function __construct(RulesRequest $request)
    {
        $cr            = new PlaneRefundRule();
        $this->request = $request;
        $this->rules   = $cr->findByCabin($request->getCarrierCode(), $request->getCabin())->toArray();
    }

    /**
     * @throws Exception
     */
    public function getRefundRules(): array
    {
        $rules         = [];
        $trip_at       = $this->request->getTripAt();
        $time_rule_eq  = "/^=(?<hour>\d+)/"; // 等于
        $time_rule_gte = "/^>=(?<hour>\d+)/";// 大于等于
        $time_rule_lte = "/^<=(?<hour>\d+)/";// 小于等于
        foreach ($this->rules as $rule) {
            $start    = $rule['start'] ?: "";
            $end      = $rule['end'] ?: "=0";
            $rule_key = "{$start}-{$end}";
            $current  = false;
            if (!$start) {
                $start_time         = '-';
                $start_hour         = '-';
                $start_time_include = true;// 任意时间
            } else {
                if (preg_match($time_rule_eq, $start, $res)) {
                    $start_hour = $res['hour'];

                    $start_time_include = true;
                } elseif (preg_match($time_rule_gte, $start, $res)) {
                    $start_hour = $res['hour'];

                    $start_time_include = true;
                } elseif (preg_match($time_rule_lte, $start, $res)) {
                    $start_hour = $res['hour'];

                    $start_time_include = true;
                } else {
                    // 纯数字
                    $start_hour = $start;

                    $start_time_include = false;
                }

                $start_time = Carbon::parse($trip_at)->subHours($start_hour)->format('Y-m-d H:i');
            }

            // 等于
            if (preg_match($time_rule_eq, $end, $res)) {
                $end_hour         = $res['hour'];
                $end_time_include = true;
            } elseif (preg_match($time_rule_gte, $start, $res)) {
                $end_hour         = $res['hour'];
                $end_time_include = true;
            } elseif (preg_match($time_rule_lte, $start, $res)) {
                $end_hour         = $res['hour'];
                $end_time_include = true;
            } else {
                // 纯数字
                $end_hour         = $res['hour'];
                $end_time_include = false;
            }
            $end_time = Carbon::parse($trip_at)->subHours($end_hour)->format('Y-m-d H:i');

            if ($start_time == '-') {
                if ($end_time_include) {

                    if (Carbon::now()->lessThanOrEqualTo($end_time)) {
                        $current = true;
                    }
                } else {
                    if (Carbon::now()->lessThan($end_time)) {
                        $current = true;
                    }
                }

            } else {

                $s = false;
                $e = false;
                if ($end_time_include) {
                    if (Carbon::now()->lessThanOrEqualTo($end_time)) {

                        $e = true;
                    }
                } else {

                    if (Carbon::now()->lessThan($end_time)) {
                        $e = true;
                    }
                }

                if ($start_time_include) {

                    if (Carbon::now()->greaterThanOrEqualTo($start_time)) {
                        $s = true;
                    }
                } else {

                    if (Carbon::now()->greaterThan($start_time)) {
                        $s = true;
                    }
                }
                $current = $s && $e;
            }


            $rate             = $rule['rate'];
            $child_rate       = $rule['child_rate'];
            $discount         = $rule['discount'];
            $desc             = $rule['rule_desc'];
            $rules[$rule_key] = [
                'start'      => [
                    'time'    => $start_time,
                    'hour'    => $start_hour,
                    'include' => $start_time_include,
                ],
                'end'        => [
                    'time'    => $end_time,
                    'hour'    => $end_hour,
                    'include' => $end_time_include,
                ],
                'current'    => $current,
                'rate'       => $rate,
                'child_rate' => $child_rate,
                'desc'       => $desc,
                'discount'   => $discount,
            ];
        }
        return array_values($rules);
    }

    public function getRefundRuleDescription(): string
    {
        return '';
    }

    public function getRefundRuleDescriptionUy(): string
    {
        return '';
    }
}